package com.zyk.leetcode;

/**
 *
 * https://leetcode-cn.com/problems/reverse-bits/
 *
 * @author zhangsan
 * @date 2021/3/29 11:01
 */
public class C190 {

    // 反转输入的整数的字节
    public static int reverseBits(int n) {
        int ans = 0;
        for (int i = 0; i < 32; i++) {
            ans = (ans<<1);
            if(  (n & (1 << i)) != 0 ) {
                ans++;
            }
        }
        return ans;
    }


    /** 一个数的因子个数如果为4 就计算他们的因子和, 否则为0
     * 返回数组中个数的因子和之和 */
    public static int sumFourDivisors(int[] nums) {
        int ans = 0;
        for (int i = 0; i < nums.length; i++) {
            int fnt_cnt = 2, fnt_sum = 1 + nums[i];
            for (int j = 2; j * j <= nums[i]; j++) {
                if( nums[i] % j == 0 ) {
                    fnt_cnt += 1;
                    fnt_sum += j;
                    if( j*j != nums[i] ) {
                        fnt_cnt += 1;
                        fnt_sum += nums[i] / j;
                    }
                }
            }
            if(fnt_cnt == 4) {
                ans += fnt_sum;
            }
        }
        return ans;
    }

    // for test
    public static void printIntegerBinary(int n) {
        // 从左往右打印
        for (int i = 31; i >= 0; i--) {
            System.out.print(  (n & (1<<i)) != 0? '1' : '0'  );
        }
        System.out.println();
    }

    // for test
    public static void main(String[] args) {
        /*int n = -3;
        int r1 = reverseBits(n);
        printIntegerBinary(n);
        printIntegerBinary(r1);
        System.out.println(r1);*/

        /*int[] nums = {
                21,4,7, 27
        };
        int r1 = sumFourDivisors(nums);
        System.out.println(r1);*/
        for (int i = 1; i <= 100; i++) {
            System.out.println(i + " : " + test(i));
        }
    }

    public static int test(int num) {
        int ans = 0;
        int fnt_cnt = 2, fnt_sum = 1 + num;
        for (int j = 2; j * j <= num; j++) {
            if( num % j == 0 ) {
                fnt_cnt += 1;
                fnt_sum += j;
                if( j*j != num ) {
                    fnt_cnt += 1;
                    fnt_sum += num / j;
                }
            }
        }
        if(fnt_cnt == 4) {
            ans = fnt_sum;
        }
        return ans;
    }


}
